package com.nullpointer.xchatfinal.fragment;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.edmodo.rangebar.RangeBar;
import com.nullpointer.xchatfinal.R;
import com.nullpointer.xchatfinal.object.UserInfo.GenderType;
import com.nullpointer.xchatfinal.utils.DataManager;
import com.nullpointer.xchatfinal.utils.KiraGameLog;

public class FindFriendSettingDialogFragment extends DialogFragment {
	private RangeBar rbAge;
	private TextView tvAge;
	private Button btAction;
	private TextView tvMale;
	private TextView tvFemale;
	private TextView tvOther;
	private GenderType gender;
	private int focus = Color.parseColor("#ff8800");
	private int normal = Color.parseColor("#333333");

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_find_friend_setting,
				container);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		gender = DataManager.getInstance(getActivity()).getGenderSearch();
		if (gender == GenderType.FEMALE) {
			tvFemale.setTextColor(focus);
			tvMale.setTextColor(normal);
			tvOther.setTextColor(normal);
		} else if (gender == GenderType.MALE) {
			tvFemale.setTextColor(normal);
			tvMale.setTextColor(focus);
			tvOther.setTextColor(normal);
		} else if (gender == GenderType.ANY) {
			tvFemale.setTextColor(normal);
			tvMale.setTextColor(normal);
			tvOther.setTextColor(focus);
		}
		rbAge.setTickHeight(0);
		rbAge.setTickCount(33);
		int leftIntIndex = DataManager.getInstance(getActivity()).getAgeMin() - 18;
		int rightIntIndex = DataManager.getInstance(getActivity()).getAgeMax() - 18;
		KiraGameLog.d("index seekbar " + leftIntIndex + "-" + rightIntIndex);
		if (leftIntIndex < 0) {
			leftIntIndex = 0;
		}
		if (rightIntIndex > 32) {
			rightIntIndex = 32;
		}
		rbAge.setThumbIndices(leftIntIndex, rightIntIndex);
		String display = "";
		if (rightIntIndex >= 32) {
			display = "Age: " + (leftIntIndex + 18) + " - 50+";
		} else {
			display = "Age: " + (leftIntIndex + 18) + " - "
					+ (rightIntIndex + 18);
		}

		tvAge.setText(display);
	}

	private void setEvent() {
		btAction.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DataManager.getInstance(getActivity()).setGenderSearch(gender);
				dismiss();
			}
		});

		tvFemale.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (gender != GenderType.FEMALE) {
					tvFemale.setTextColor(focus);
					tvMale.setTextColor(normal);
					tvOther.setTextColor(normal);
					gender = GenderType.FEMALE;
				}

			}
		});
		tvMale.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (gender != GenderType.MALE) {
					tvFemale.setTextColor(normal);
					tvMale.setTextColor(focus);
					tvOther.setTextColor(normal);
					gender = GenderType.MALE;
				}
			}
		});
		tvOther.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (gender != GenderType.ANY) {
					tvFemale.setTextColor(normal);
					tvMale.setTextColor(normal);
					tvOther.setTextColor(focus);
					gender = GenderType.ANY;
				}
			}
		});

		// Sets the display values of the indices
		rbAge.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
			@Override
			public void onIndexChangeListener(RangeBar rangeBar,
					int leftThumbIndex, int rightThumbIndex) {
				DataManager.getInstance(getActivity()).setAgeMin(
						leftThumbIndex + 18);
				DataManager.getInstance(getActivity()).setAgeMax(
						rightThumbIndex + 18);
				String display = "";
				if (rightThumbIndex >= 32) {
					display = "Age: " + (leftThumbIndex + 18) + " - 50+";
				} else {
					display = "Age: " + (leftThumbIndex + 18) + " - "
							+ (rightThumbIndex + 18);
				}

				tvAge.setText(display);
			}
		});
	}

	private void findViewIds(View view) {
		rbAge = (RangeBar) view.findViewById(R.id.rb_age);
		btAction = (Button) view.findViewById(R.id.bt_action);
		tvMale = (TextView) view.findViewById(R.id.tv_male);
		tvFemale = (TextView) view.findViewById(R.id.tv_female);
		tvOther = (TextView) view.findViewById(R.id.tv_other);
		tvAge = (TextView) view.findViewById(R.id.tv_age);
	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		super.onDismiss(dialog);
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		super.onCancel(dialog);
	}
}
